﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Shapes;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Core;
namespace Centny
{
    public class ExtWebView : ExtBaseGrid
    {
        private WebView _WView;
        public WebView WView
        {
            get
            {
                return this._WView;
            }
        }
        private Rectangle _Rect;
        public Rectangle Rect
        {
            get
            {
                return this._Rect;
            }
        }
        public ExtWebView()
        {
            this._Rect = new Rectangle();
        }
        protected override void OnLoaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            base.OnLoaded(sender, e);
            if (this.Children.Count < 1)
            {
                return;
            }
            this._WView = this.Children[0] as WebView;
            if (this.WView == null)
            {
                throw new InvalidCastException("the ExtWebView content is not a WebView");
            }
            this.Children.Add(this.Rect);
            Window.Current.Activated += Current_Activated;
            this.WView.LoadCompleted += WViewLoadCompleted;
            this.Rect.Visibility = Visibility.Collapsed;
            this.WView.Visibility = Visibility.Visible;
            //this.Rect.PointerEntered += OnRectPointerEvent;
            //this.Rect.PointerMoved += OnRectPointerEvent;
            //this.Rect.PointerPressed += OnRectPointerEvent;

        }
        private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
            {
                this.LoadContentFake();
            }
            else
            {
                this.HideContenFake();
            }
        }
        private void OnRectPointerEvent(object sender, PointerRoutedEventArgs e)
        {
            this.HideContenFake();
        }
        private int FakeTimes = 0;
        public async void HideContenFake()
        {
            if (this.FakeTimes > 1)
            {
                this.FakeTimes--;
                System.Diagnostics.Debug.WriteLine("HideContenFake:" + this.FakeTimes);
                return;
            }
            if (this.FakeTimes == 1)
            {
                this.FakeTimes--;
            }
            this.WView.Visibility = Visibility.Visible;
            await Task.Delay(100);
            this.Rect.Visibility = Visibility.Collapsed;
            System.Diagnostics.Debug.WriteLine("HideContenFake:" + this.FakeTimes);
        }
        public async void LoadContentFake()
        {
            if (this.FakeTimes > 0)
            {
                this.FakeTimes++;
                System.Diagnostics.Debug.WriteLine("LoadContentFake:" + this.FakeTimes);
                return;
            }
            this.FakeTimes++;
            WebViewBrush b = new WebViewBrush();
            b.SetSource(this.WView);
            b.Redraw();
            this.Rect.Fill = b;
            this.Rect.Visibility = Visibility.Visible;
            await Task.Delay(100);
            this.WView.Visibility = Visibility.Collapsed;
            System.Diagnostics.Debug.WriteLine("LoadContentFake:" + this.FakeTimes);
        }
        private void FocusWView()
        {
            try
            {
                String js = "try{";
                js += "document.getElementsByTagName('body')[0].focus();";
                js += "}catch(e){}";
                this.WView.InvokeScript("eval", new String[] { js });
            }
            catch (System.Exception except)
            {

            }
        }
        //private void FocusSv()
        //{
        //    IEnumerable<Control> cs = VisualTreeHelperExtensions.GetAncestorsOfType<Control>(this.WView);
        //    int i = 0;
        //    foreach (Control c in cs)
        //    {
        //        if (i == 2)
        //            c.Focus(FocusState.Pointer);
        //        i++;
        //    }
        //}
        private void WViewLoadCompleted(object sender, NavigationEventArgs e)
        {
            this.FocusWView();
        }
    }
}
